# -*- coding:utf-8-*-
import urllib.request
import urllib.parse
import json

content = input("请输入需要翻译的内容：")
url = "http://fanyi.youdao.com/translate?smartresult = dict&smartresult=rule&smartresult=" \
      "ugc&sessionFrom = http://www.youdao.com/"
data = {}
data['type'] = 'AUTO'
data['i'] = 'I love FishC.com!'
data['doctype'] = 'json'
data['xmlVersion'] = '1.6'
data['keyfrom'] = 'fanyi.web'
data['ue'] = 'UTF-8'
data['typeResult'] = 'true'
data = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.urlopen(url, data)
html = response.read().decode('utf-8')
print(html)
# 编码问题：http://bbs.fishc.com/thread-56452-1-1.html
target = json.loads(html)  # 解析JSON格式字符串
# type(target)
print("翻译结果是：%s" % (target['translateResult'][0][0]['tgt']))
